layui.use(['form','table','laypage'], function(){
  var table = layui.table;
  var form = layui.form,
	layer = layui.layer;
    console.log(ty);
    var datacols = [];
    if(ty == 0)
    {
        datacols = [[
           {type: 'checkbox', fixed: 'left'}
		  ,{field:'activity_id', title: '序号'}
		  ,{field:'act_name', title: '活动名称'} 
		  ,{field:'time', title: '活动有效期'}
		  ,{field:'mz_money', title: '满足金额'}
		  ,{field:'yh_money', title: '优惠金额'}
		  ,{field:'reason', title: '操作'}
		]];
    }
    if(ty == 1)
    {
        datacols = [[
           {type: 'checkbox', fixed: 'left'}
		  ,{field:'activity_id', title: '序号'}
		  ,{field:'act_name', title: '活动名称'} 
		  ,{field:'time', title: '活动有效期'}
		  ,{field:'zs_goods', title: '产品名称'}
		  ,{field:'number', title: '满足数量'}
          ,{field:'discount', title: '折扣'}
		  ,{field:'reason', title: '操作'}
		]];
    }
    if(ty == 2)
    {
        datacols = [[
           {type: 'checkbox', fixed: 'left'}
		  ,{field:'activity_id', title: '序号'}
		  ,{field:'act_name', title: '活动名称'} 
		  ,{field:'time', title: '活动有效期'}
		  ,{field:'mz_money', title: '满足金额'}
		  ,{field:'zs_goods', title: '产品名称'}
		  ,{field:'reason', title: '操作'}
		]];
    }
    if(ty == 3)
    {
        datacols = [[
           {type: 'checkbox', fixed: 'left'}
		  ,{field:'activity_id', title: '序号'}
		  ,{field:'act_name', title: '活动名称'} 
		  ,{field:'time', title: '活动有效期'}
		  ,{field:'zs_goods', title: '产品名称'}
		  ,{field:'discount', title: '折扣'}
		  ,{field:'reason', title: '操作'}
		]];
    }
    
    
	/*初始化表格*/
	function tableInit()
	{  
	  table.render({
		elem: '#dataTables-example'
		,url:'/shop/activity/mjactivlist.html'
		,where:{ty:ty}
		,method:'POST'
		,parseData: function(res){ //res 即为原始返回的数据
			//封装返回数据
			console.log(res);
			var urls = '';
			
			for (i in res.data) {
				urls = '/shop/activity/editactivity/id/'+res.data[i].activity_id+'/ty/'+ty+'.html';
                res.data[i]['reason'] = "<button class='layui-btn' onclick=\"xadmin.open('编辑活动','"+urls+"',1000,600)\">编辑</button>";
                res.data[i]['reason'] += "<button class='layui-btn' onclick=\"xadmin.open('发送短息','"+urls+"',1000,600)\">发送短息</button>";
                //res.data[i]['reason'] += "<button class='layui-btn delAdmin' rid='"+res.data[i].activity_id+"'>删除</button>";
                if(res.data[i].act_type == 3)
                {
                    var strgoods = '';
                    for (j in res.data[i].zs_goods) {
                        strgoods += '<p>'+res.data[i].zs_goods[j]+'</p>';
                    }
                    res.data[i]['zs_goods'] = strgoods;
                }
            }
			return {
			  "code": 10001, //解析接口状态
			  "msg": '', //解析提示文本
			  "count": res.total, //解析数据长度
			  "data": res.data //解析数据列表
			};
		}
		,cols: datacols
		,page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
		  layout: ['prev', 'page', 'next', 'skip', 'count']
		}
		,response: {
			statusCode: 10001
		}
	  });
	}
	tableInit();
	  //删除请假
	$('body').on('click', '.delAdmin', function(){
		var get_this = $(this);
		parent.layer.confirm('你确定要删除该条信息吗?', function(){
			var params = {};
			params.id = get_this.attr('rid');
			$.ajax({
				type: "POST",
				url: "/shop/activity/delactivity.html",
				cache: false, //禁用缓存
				data: params, //传入组装的参数
				dataType: "json",
				success: function (result) {
					if(result.code==10001){
                        xadmin.father_reload();
						parent.layer.msg('操作成功', {icon: 1});
					}else{
						parent.layer.msg(result.msg, {icon: 5});
					}
				},
				error:function () {
					parent.layer.msg('网络错误', {icon: 5});
				}
			})
		});
	});
	$('body').on('click', '#delActiv', function(){
		//获取选中数据
      var checkStatus = table.checkStatus('dataTables-example')
      ,data = checkStatus.data;
	  console.log(data);
	  if(data.length == 0)
	  {
		  parent.layer.msg('未选择数据', {icon: 5});
		  return false;
	  }
	  var ids = '';
	  for (i in data) 
	  {
		ids += data[i].activity_id + ',';
	  }
	  ids=ids.substring(0,ids.length-1);
	  var params = {};
        params.id = ids;
        $.ajax({
            type: "POST",
            url: "/shop/activity/delactivity.html",
            cache: false, //禁用缓存
            data: params, //传入组装的参数
            dataType: "json",
            success: function (result) {
                if(result.code==10001){
                    xadmin.father_reload();
                    parent.layer.msg('操作成功', {icon: 1});
                }else{
                    parent.layer.msg(result.msg, {icon: 5});
                }
            },
            error:function () {
                parent.layer.msg('网络错误', {icon: 5});
            }
        })
	});
});

